import itertools
import os
import shutil
from pathlib import Path

import win32com.client

from tools_pmc import (today_str1, today_str2, lj_JH0020
                       )


def flatten_list(lst):
    return list(itertools.chain(*lst))


set_lj = {'BOM.xls',
          'JH0020.xls',
          'JH0022.xls',
          '今日库存明细账.xlsx',
          '取替代料明细表.xlsx',
          '工单完工情况报表.xls',
          '欠料信息表.xlsx',
          '物料信息表.xls',
          '请购明细表.xlsx',
          '通用物料判定表1.xls',
          '通用物料判定表2.xls'
          }
if set_lj <= set(os.listdir(r"\\192.168.70.101\19计划管理部\01.计划\1，数据处理工具\file\Temp")):
    print("Temp文件存在，执行remame程序")
else:
    print("Temp文件不存在，跳过rename程序")
    exit()

try:
    shutil.rmtree("//192.168.70.101/19计划管理部/01.计划/1，数据处理工具/file/" + today_str1)
except FileNotFoundError:
    pass

# 创建文件夹
lj_source_file = "//192.168.70.101/19计划管理部/01.计划/5，物控管理/3，物料状态管理/data/" + today_str1
folderPath = Path(lj_source_file)
folderPath.mkdir(parents=True, exist_ok=True)

lj_source_file = "//192.168.70.101/19计划管理部/01.计划/1，数据处理工具/file/" + today_str1 + "/source_file_Ⅱ/source_file_Ⅰ"
folderPath = Path(lj_source_file)
folderPath.mkdir(parents=True, exist_ok=True)

# # 文件转移
list_name = ["BOM.xls",
             "工单完工情况报表.xls",
             "欠料信息表.xlsx",
             "请购明细表.xlsx",
             "取替代料明细表.xlsx",
             "通用物料判定表1.xls",
             "通用物料判定表2.xls",
             "物料信息表.xls",
             "今日库存明细账.xlsx",
             "JH0022.xls",
             "JH0020.xls"
             ]
for str1 in list_name:
    shutil.copy2("//192.168.70.101/19计划管理部/01.计划/1，数据处理工具/file/Temp/" + str1, lj_source_file)

# 名称转换
os.chdir(lj_source_file)  # 改变工作目录_
list_name_new = ["BOM.xls",
                 "工单完工情况报表" + today_str1 + ".xls",
                 "欠料信息表" + today_str1 + ".xlsx",
                 "请购明细表" + today_str1 + ".xlsx",
                 "取替代料明细表" + today_str1 + ".xlsx",
                 "通用物料判定表1.xls",
                 "通用物料判定表2.xls",
                 "物料信息表" + today_str1 + ".xls",
                 "今日库存明细账" + today_str1 + ".xlsx",
                 "JH0022.xls",
                 "JH0020.xls"
                 ]
i = 0
for str1 in list_name:
    os.rename(str1, list_name_new[i])
    i += 1

lj_source_file = "//192.168.70.101/19计划管理部/01.计划/1，数据处理工具/file/" + today_str1 + "/source_file_Ⅱ/source_file_Ⅰ"
file_ls = []
for root, dirs, files in os.walk(lj_source_file):
    root_file_ls = [os.path.join(root, file) for file in files]
    file_ls.append(root_file_ls)

result_ls = flatten_list(file_ls)


def excelapi_主任务():
    excel = win32com.client.DispatchEx('Excel.application')
    excel.Visible = False
    excel.DisplayAlerts = False
    for lj_source_file in result_ls:
        lj_source_file = lj_source_file.replace('\\', '/')
        if lj_source_file[-1] == "x":
            shutil.move(lj_source_file,
                        "//192.168.70.101/19计划管理部/01.计划/1，数据处理工具/file/" + today_str1 + "/source_file_Ⅱ")
        else:

            e_wb = excel.Workbooks.Open(lj_source_file)
            file_Name = os.path.splitext(lj_source_file)
            new_excel_path = file_Name[0] + ".xlsx"
            e_wb.SaveAs(new_excel_path, FileFormat=51)
            e_wb.Close()
            shutil.move(lj_source_file + "x",
                        "//192.168.70.101/19计划管理部/01.计划/1，数据处理工具/file/" + today_str1 + "/source_file_Ⅱ")
    excel.Application.Quit()


try:
    excelapi_主任务()
except AttributeError:
    shutil.rmtree(r"C:\Users\10352\AppData\Local\Temp\gen_py\3.12")
    excelapi_主任务()

shutil.copy(lj_JH0020, "//192.168.70.101/19计划管理部/01.计划/5，物控管理/3，物料状态管理/data/" + today_str1)



